
$(function ()
{
    var monthly_price = $('#monthly_price').val();

    var sales_tax = $('#sales_tax').val();
	var need_sales_tax = 0;
	var total_value = 0;
	var currency_short = $('#currency_short').val();
	var currency_symbol = $('#currency_symbol').val();
	var confirmation_tab_signal = 0;
	
	$('#login').click(function(){
		  $('#login_form').submit();
	});
    function toggle_steps($id_step)
    {
		$("#step").hide();
		$("#step0").hide();
        $("#step1").hide();
        $("#step2").hide();
        $("#step3").hide();
        $("#step4").hide();
        $("#step5").hide();
        $("#step6").hide();
		$("#step61").hide();
		$("#step62").hide();
		$('#calback_form').hide();
		/*if ($id_step == '#step1') {
			$('#calback_form').show();
		}*/
        $($id_step).show();
	
        var li_toggle=$id_step+"li";
        $('#step0li').removeClass("active");
        $('#step1li').removeClass("active");
        $('#step2li').removeClass("active");
        $('#step3li').removeClass("active");
        $('#step4li').removeClass("active");
        $('#step5li').removeClass("active");
        $('#step6li').removeClass("active");
        if ( li_toggle == '#step6li' )
            $(step4li).addClass("active");
        else
            $(li_toggle).addClass("active");
    }

    $('#order_inform > div > p > input ').each(function(){
        $(this).bind('click', setProductPrices);
    });
    

    function setProductPrices()
    {
        /*var first = true;
        $('#order_inform > div > p > input ').each(function(){
            if ( $(this).attr('checked') )
            {
//                price = product_price;
                if (need_sales_tax == 1) {			
					var sales_tax_total = Number(product_price) * (Number(sales_tax) / 100);
					var sales_tax_monthly = Number(monthly_price) * (Number(sales_tax) / 100);
					product_price = Number(product_price) + sales_tax_total;
					monthly_value = Number(monthly_price) + sales_tax_monthly;
				}// else {
				//	total_value = Number(price.build) - discount;
				//	monthly_value = Number(price.hosting);
				//}
                if ( first)
                {
                    product_price += product_price_diff;
                    first = false;
                }
                if (need_sales_tax == 1) {
					sales_tax_amount = Number(product_price) * (Number(sales_tax) / 100);
					$('#show_sales_tax_p').show();
					$('#details_sales_tax_p').show();
					$('#show_sales_tax').html(' $'+sales_tax_amount.toFixed(2)+' ('+sales_tax+'%)');
					$('#details_sales_tax').html(' $'+sales_tax_amount.toFixed(2)+' ('+sales_tax+'%)');
				} else {
					$('#show_sales_tax_p').hide();
					$('#details_sales_tax_p').hide();
				}
                
                $('#label_'+$(this).attr('id')).html($(this).attr('alt')+' (£'+product_price.toFixed(2)+')');
            }
            else
                $('#label_'+$(this).attr('id')).html($(this).attr('alt'));
            
        });*/
        //console.log(need_sales_tax);
        //console.log(sales_tax);
        
        
        if (need_sales_tax == 1) {			
			var sales_tax_monthly = Number(3*monthly_price) * (Number(sales_tax) / 100);
			total_value = Number(3*monthly_price) + sales_tax_monthly;
			$('#tr_sales_tax').show();
			//$('#details_sales_tax_p').show();
			$('#show_sales_tax').html(' $'+sales_tax_monthly.toFixed(2)+' ('+sales_tax+'%)');
			//$('#details_sales_tax').html(' $'+sales_tax_monthly.toFixed(2)+' ('+sales_tax+'%)');
		} else {
			$('#tr_sales_tax').hide();
			//$('#details_sales_tax_p').hide();
			total_value = Number(3*monthly_price)
		}
		$('#show_total').text(' '+currency_symbol+total_value.toFixed(2)+' per quarter');
		//$('#details_total').html(' '+currency_symbol+total_value.toFixed(2));
		//$('#ao_total_cost').val(total_value.toFixed(2));
    }

	

    toggle_steps("#step0");

    $('#step1li').click(function()
    {
        toggle_steps('#step1');
        $('#step').show();
    });
    $('#step2li').click(function()
    {
		if (confirmation_tab_signal == 1) {
			toggle_steps('#step2');
			$('#step').show();
		}
    });

   
    $('#submit7').click(function(){
        toggle_steps('#step1');
        $('#step').show();
    });
    $('#submit71').click(function(){
        toggle_steps('#step2');
        $('#step').show();
    });
	$('#submit72').click(function(){
        toggle_steps('#step1');
        $('#step').show();
    });
    $('#step0li').click(function()
    {
        toggle_steps('#step0');
    });
    
     $('#step3li').click(function()
    {
        toggle_steps('#step1');
    });

    $('#logo').click(function(){
        toggle_steps("#step0");
    });

    $('#proceed_to_checkout, #proceed_to_checkout2, #proceed_to_checkout3').click(function(){
        toggle_steps("#step1");
		$('#step').show();
    });
	$('#termlink').click(function(){
        toggle_steps("#step6");
		
    });
    $('#tandc').click(function(){
        toggle_steps('#step61');
    });
	$('#privacylink').click(function(){
        toggle_steps("#step62");
		
    });
    $('#print_button').click(function(){
        $('#step5').printElement();
    });
 
    function show_input($var_input)
    {
        $($var_input).show("100");
	
    }

    function hide_input($var_input)
    {
        $($var_input).hide("100");
    }

    $('.Yes').click(function() {
        show_input('#sh1');
    });
    $('.No').click(function() {
        hide_input('#sh1');
    });

    var valids= $("#first_form").validate();
	 
    $("#sbt").click(function()
    {//email validation
        $.post("send_notification_mail.php",$("#first_form").serialize());

        $('#upload').show();
        //$('#enterpass').show();
        //$('#calback_form').hide();

        $('#callback_company_email').val($('#email').val());
        $.scrollTo('div[id=step1_2]', 1000); 
    });

    $('#upload_yes').click(function(){
        $('#hidebtn').show();
        $('#calback_form').hide();
        //$('#calback_form').show();
        //$('#enterpass').hide();
    });

    $('#btn_member').click(function(){
        $('#callback_msg').html('');

        /*if ($('#name_member').val() == '' )
        {
            $('#name_member').after('<label for="email" generated="true" class="error">This field is required.</label>');
            valid = false;
        }*/
        var valids = $("#calback_form").validate();
        if( valids.element('#name_member') && valids.element('#company_member') && valids.element('#telephone_member') && valids.element('#time_member'))
        {
				/*var firstname = $.trim($("#house_number").val().toUpperCase());
                firstname = firstname.replace(/ |-/g, '');	
                var surname = $.trim($("#surename").val().toUpperCase());
                surname = surname.replace(/ |-/g, '');	
                var ref_id = firstname+surname;
                if (ref_id.length > 10) {
                	ref_id = ref_id.substr(0,10);
                } 
				$('#ref_id').val(ref_id);*/
            $.post("call_back.php",$("#calback_form").serialize(),function(data, status)
            {
            
            });
            $('#callback_msg').html('Thanks for contacting us, we will be in touch shortly.');
            //toggle_steps("#step3");
            //$('#calback_form').hide();
        }
    });

    $('#upload_no').click(function(){
        $('#calback_form').show();
        //$('#enterpass').hide();
		 $('#hidebtn').hide();
		$.scrollTo( 'div[id=step1_3]', 1000 ); 
    });

    $("#hidebtn").click(function()
    {//password validation
        
        if ( $('#account_yes').attr('checked') ){//&& $('#sh1').val() == '' ){
            $('#sh1_error').show();
            return;
        }
        $('#sh1_error').hide();
        $('#step2li').bind('click', function() {
            toggle_steps('#step3');
        });
               
        toggle_steps("#step3");
        
    });


    function add_tx($var_where,$var_val)
    {//id to, id from
        var val_txt=$($var_val).val();
        $($var_where).text($($var_val).val());
		
    }

    function add_cb($var_where,$var_val)
    {
        var val_cb_t=$($var_val).attr('checked');
        var text = 'No';
        if ( val_cb_t || val_cb_t == 'true' )
            text = 'Yes';
        $($var_where).text(text);
    }
    
    function add_sel(var_where,var_val){//id to, id from

		$(var_where).text($(var_val).find("option:selected").text());
	}

    function fill_forms(begin)
    {
        //add_tx('#'+begin+'_email','#email');//email

        /*var app_count = 0;
        $('#order_inform > div > p > input ').each(function()
        {
            if ( $(this).attr( "checked" ) )
            {
                app_count++;
                var text = '';
                /*if ( app_count == 1)
                    text += ' (£'+((product_price+product_price_diff).toFixed(2))+')';
                else
                    text += ' (£'+product_price.toFixed(2)+')';
                $('#'+begin+'_'+$(this).attr( "name" )).text('Yes'+text);
            }
            else
                $('#'+begin+'_'+$(this).attr( "name" )).text('No');

        });*/


        add_tx('#'+begin+'_company','#companyname');//company
        add_tx('#'+begin+'_firstname','#firstname');//firstname
        add_tx('#'+begin+'_surname','#surname');//surname
        add_tx('#'+begin+'_email','#emailaddress');//emailaddress
        
        add_tx('#'+begin+'_tel','#officetelephone');//officetelephone
		add_tx('#'+begin+'_mobile','#cellphonenumber');//phone
        add_tx('#'+begin+'_website','#website');//site

        add_tx('#'+begin+'_address1','#addressline_one');//addressline_one
        add_tx('#'+begin+'_address2','#addressline_two');//addressline_two
        add_tx('#'+begin+'_town','#town');//town
        add_tx('#'+begin+'_zipcode','#zipcode');//zipcode
        
        add_sel('#'+begin+'_state','#state');//state
		add_sel('#'+begin+'_country','#country');//coutry
		
        //add_tx('#'+begin+'_username','#username');//username
		//add_cb('#'+begin+'_monthly_payment','#monthly_payment')//monthly payment
        //Calculating total value of the products
        //var total = product_price*app_count+product_price_diff;
        //return total.toFixed(2);
    }
	
	$("#contact_btn").click(function() {//contact form
		var  valids= $("#contact_form2").validate();
		if (valids.form()){
			confirmation_tab_signal = 1;
			
			var state_val = $('#state option:selected').text();
					
			if ($.trim(state_val) == 'New York') {
				need_sales_tax = 1;
			} else {
				need_sales_tax = 0;
			}
			setProductPrices();
			fill_forms('show');
			toggle_steps("#step2");
			$('#step').show();
		}else {
			alert('Please fill all required field');
		}
	});
	
	$("#confirmation_btn").click(function() {//confirmation
		if (!$('#agree1').attr('checked') )
        {
            alert('Please accept our Terms & Conditions before proceeding.');
            return;
        }
        
		$.post("save_order.php",$("#contact_form2").serialize(),function(data) {
			if (data == true) {
				$("#contact_form2")[0].reset();
				//location.reload();
				
				var i = 1;
				var site_name = 'SIBOR App'
				var product_name = 'SIBOR App  -  Agent Package'        
				var monthly_value = monthly_price;
				product_no = 1;
				
				$('#charset').after('<input type="hidden" name="shopping-cart.items.item-'+product_no+'.item-name" value="'+site_name+'"/>');
				$('#charset').after('<input type="hidden" name="shopping-cart.items.item-'+product_no+'.item-description" value="SIBOR App - Agent Package (Quarterly Payment in Advance)"/>');
				$('#charset').after('<input type="hidden" name="shopping-cart.items.item-'+product_no+'.unit-price" value="'+total_value+'"/>');
				$('#charset').after('<input type="hidden" name="shopping-cart.items.item-'+product_no+'.quantity" value="1"/>');
				$('#charset').after('<input type="hidden" name="shopping-cart.items.item-'+product_no+'.unit-price.currency" value="'+currency_short+'"/>');
				
				if ($('#agree2').attr('checked') ){
					product_no++;
					$('#charset').after('<input type="hidden" name="shopping-cart.items.item-'+product_no+'.item-name" value="Subscription to '+site_name+'"/>');
					$('#charset').after('<input type="hidden" name="shopping-cart.items.item-'+product_no+'.item-description" value="SIBOR App - Agent Package (Standard Quarterly Payment)"/>')
					$('#charset').after('<input type="hidden" name="shopping-cart.items.item-'+product_no+'.unit-price.currency" value="'+currency_short+'"/>');
					$('#charset').after('<input type="hidden" name="shopping-cart.items.item-'+product_no+'.unit-price" value="0.00"/>');	    
					$('#charset').after('<input type="hidden" name="shopping-cart.items.item-'+product_no+'.quantity" value="1"/>');
					$('#charset').after('<input type="hidden" name="shopping-cart.items.item-'+product_no+'.subscription.type" value="google"/>');
					$('#charset').after('<input type="hidden" name="shopping-cart.items.item-'+product_no+'.subscription.period" value="QUARTERLY"/>');
					//$('#charset').after('<input type="hidden" name="shopping-cart.items.item-'+product_no+'.subscription.payments.subscription-payment-1.times" value="12">');
					$('#charset').after('<input type="hidden" name="shopping-cart.items.item-'+product_no+'.subscription.payments.subscription-payment-1.maximum-charge" value="'+total_value+'">');
					$('#charset').after('<input type="hidden" name="shopping-cart.items.item-'+product_no+'.subscription.payments.subscription-payment-1.maximum-charge.currency" value="'+currency_short+'">');
					$('#charset').after('<input type="hidden" name="shopping-cart.items.item-'+product_no+'.subscription.recurrent-item.item-name" value="Hosting"/>')
					$('#charset').after('<input type="hidden" name="shopping-cart.items.item-'+product_no+'.subscription.recurrent-item.item-description" value="Hosting"/>')
					$('#charset').after('<input type="hidden" name="shopping-cart.items.item-'+product_no+'.subscription.recurrent-item.quantity" value="1"/>')	      
					$('#charset').after('<input type="hidden" name="shopping-cart.items.item-'+product_no+'.subscription.recurrent-item.unit-price" value="'+total_value+'"/>')	      
					$('#charset').after('<input type="hidden" name="shopping-cart.items.item-'+product_no+'.subscription.recurrent-item.unit-price.currency" value="'+currency_short+'"/>')
				}
				$('#BB_BuyButtonForm').submit();
				
			} else if (data == false) {
				alert('Sorry, this name already exists in our system. Please contact us to resolve this issue.');
			} else {
				alert('Some problem occured. Please try again.');
			}
		});
	});
				
    $("#submit3").click(function()
    {//addres detail validation
	
        if ((valids.element('#company_name'))&& (valids.element('#house_number'))&& (valids.element('#telephone')) &&  (valids.element('#website'))  )
        {
            $('#submit3').hide();
//            $('#show_total').text('£'+total+' (plus £'+monthly_price+' per month)');
        
            $.post("save_order.php",$("#first_form").serialize(),function(data, status)
            {
                $('#submit3').show();
                if ( data == 'true')
                {
                	var state_val = $('#state option:selected').text();
					
					if ($.trim(state_val) == 'New York') {
						need_sales_tax = 1;
					}
					setProductPrices();
					fill_forms('show');
                    $('#username_error').hide();
                    $('#step4li').bind('click', function() {
                        toggle_steps("#step4");
                    });
                    toggle_steps("#step4");
                }
                else
                {
                    $('#username_error').show();
                    $('#username_error').html(data);
                }
                    
            /*else
                    alert('There was an error saving the data');*/
            });
             
        }
    });

    $("#submit4").click(function()
    {//agree validation

        if (!$('#agree1').attr('checked') )
        {
            alert('Please accept our Terms & Conditions before proceeding.');
            return;
        }

        fill_forms('details');

        var i = 1;
        var site_name = 'SIBOR App'
        var product_name = 'SIBOR App  -  Agent Package'        
        var monthly_value = monthly_price;
		product_no = 1;
			
			$('#charset').after('<input type="hidden" name="shopping-cart.items.item-'+product_no+'.item-name" value="'+site_name+'"/>');
			$('#charset').after('<input type="hidden" name="shopping-cart.items.item-'+product_no+'.item-description" value="SIBOR App - Agent Package (Quarterly Payment in Advance)"/>');
			$('#charset').after('<input type="hidden" name="shopping-cart.items.item-'+product_no+'.unit-price" value="'+total_value+'"/>');
			$('#charset').after('<input type="hidden" name="shopping-cart.items.item-'+product_no+'.quantity" value="1"/>');
			$('#charset').after('<input type="hidden" name="shopping-cart.items.item-'+product_no+'.unit-price.currency" value="'+currency_short+'"/>');
			
			//if ( $('#monthly_payment').attr('checked')){ 
				/*product_no = 2;
				$('#charset').after('<input type="hidden" name="shopping-cart.items.item-'+product_no+'.item-name" value="1st Quarterly Payment"/>');
			  	$('#charset').after('<input type="hidden" name="shopping-cart.items.item-'+product_no+'.item-description" value=""/>');
			    $('#charset').after('<input type="hidden" name="shopping-cart.items.item-'+product_no+'.unit-price" value="'+3*monthly_value+'"/>')	      
			    $('#charset').after('<input type="hidden" name="shopping-cart.items.item-'+product_no+'.quantity" value="1"/>');
			    $('#charset').after('<input type="hidden" name="shopping-cart.items.item-'+product_no+'.unit-price.currency" value="'+currency_short+'"/>');
   				product_no = 3;*/
				product_no++;
				$('#charset').after('<input type="hidden" name="shopping-cart.items.item-'+product_no+'.item-name" value="Subscription to '+site_name+'"/>');
				$('#charset').after('<input type="hidden" name="shopping-cart.items.item-'+product_no+'.item-description" value="SIBOR App - Agent Package (Standard Quarterly Payment)"/>')
				$('#charset').after('<input type="hidden" name="shopping-cart.items.item-'+product_no+'.unit-price.currency" value="'+currency_short+'"/>');
				$('#charset').after('<input type="hidden" name="shopping-cart.items.item-'+product_no+'.unit-price" value="0.00"/>');	    
				$('#charset').after('<input type="hidden" name="shopping-cart.items.item-'+product_no+'.quantity" value="1"/>');
				$('#charset').after('<input type="hidden" name="shopping-cart.items.item-'+product_no+'.subscription.type" value="google"/>');
				$('#charset').after('<input type="hidden" name="shopping-cart.items.item-'+product_no+'.subscription.period" value="QUARTERLY"/>');
				//$('#charset').after('<input type="hidden" name="shopping-cart.items.item-'+product_no+'.subscription.payments.subscription-payment-1.times" value="12">');
				$('#charset').after('<input type="hidden" name="shopping-cart.items.item-'+product_no+'.subscription.payments.subscription-payment-1.maximum-charge" value="'+total_value+'">');
				$('#charset').after('<input type="hidden" name="shopping-cart.items.item-'+product_no+'.subscription.payments.subscription-payment-1.maximum-charge.currency" value="'+currency_short+'">');
				$('#charset').after('<input type="hidden" name="shopping-cart.items.item-'+product_no+'.subscription.recurrent-item.item-name" value="Hosting"/>')
				$('#charset').after('<input type="hidden" name="shopping-cart.items.item-'+product_no+'.subscription.recurrent-item.item-description" value="Hosting"/>')
				$('#charset').after('<input type="hidden" name="shopping-cart.items.item-'+product_no+'.subscription.recurrent-item.quantity" value="1"/>')	      
				$('#charset').after('<input type="hidden" name="shopping-cart.items.item-'+product_no+'.subscription.recurrent-item.unit-price" value="'+total_value+'"/>')	      
				$('#charset').after('<input type="hidden" name="shopping-cart.items.item-'+product_no+'.subscription.recurrent-item.unit-price.currency" value="'+currency_short+'"/>')
			//}
	    
        	$('#BB_BuyButtonForm').submit();

        //}
        if ( $('#agree3').attr('checked') || $('#agree4').attr('checked'))
        {
            $('#step5li').bind('click', function() {
                toggle_steps("#step5");
            });
            toggle_steps("#step5");
            $('#order_details').show();
            $('#details_total').text('£'+total+' (plus £'+monthly_price+' per month)');
            if ( $('#agree3').attr('checked'))
            {
                $('#cheque').show();
                $('#cheque_total').text(total);
            }
                
            if ( $('#agree4').attr('checked'))
            {
                $('#bacs').show();
            }
                

        }

                

    });

    $("#submit5").click(function()
    {//agree payment
        if (($('#agree2').attr("checked")) || ($('#agree3').attr("checked")) || ($('#agree4').attr("checked")) )
        {
            if (valids.form())
            {
                toggle_steps("#step6"); //of temporary
            }
            else
            {
                alert('Some required fields are not correctly filled in.');
            }
        }
        else
        {
            alert("Please select one of the payment options.");
        }

    });

    $("#Submit2").click(function()
    {//select app
        validateProducts();
    });

    $("#submitpayment").click(function()
    {
        if ( !validateProducts() )
            return;
        if ( validatePayments() )
        {
	   if ( $('#agree3').attr('checked') || $('#agree4').attr('checked')){
		$('#monthly_payment_p').hide();
		$('#show_monthly_payment_p').hide();
		$('#details_monthly_payment_p').hide();
	   }
	   else{
		$('#monthly_payment_p').show();
		$('#show_monthly_payment_p').show();
		$('#details_monthly_payment_p').show();
	   }
            $('#step3li').bind('click', function() {
                toggle_steps("#step3");
            });
            toggle_steps("#step3");

        }
    });

    function validateProducts()
    {
        var checked = false
        $('#order_inform > div > p > input ').each(function()
        {
            if ( $(this).attr( "checked" ) )
                checked = true;
        });

        if ( checked )
            $("#payments").show();
        else
            alert("Please select one of the Apps.");
        return checked;
    }

    function validatePayments()
    {
        var checked = false
        $('#payments > div > div  > p > input ').each(function()
        {
            if ( $(this).attr( "checked" ) )
                checked = true;
        });

        if ( !checked )
            alert("Please select one of the Payment Methods.");
        return checked;
    }

});
 
